package com.gitee.lp;


import com.gitee.lp.convert.ConvertTabel;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;


public class App {


    public static void main(String[] args) throws IOException {
        // 获取Oracle
        SqlSessionFactory oracleSessionFactory = new SqlSessionFactoryBuilder().build(
                Resources.getResourceAsReader("mybatis-config-oracle.xml"),
                "userDefine");
        // 获取DM
        SqlSessionFactory dmSessionFactory = new SqlSessionFactoryBuilder().build(
                Resources.getResourceAsReader("mybatis-config-dm.xml"),
                "userDefine");
        JFrame jFrame = new JFrame("数据同步");
        jFrame.setSize(800, 600);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jPanel = new JPanel();
        JScrollPane jScrollPane = new JScrollPane(jPanel);
        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
        for (String table : AppManager.tables) {
            JLabel label = new JLabel("同步表 " + table + " 数据：");
            JButton startYb = new JButton("开始同步 - 游标");
            JButton startFy = new JButton("开始同步 - 分页");
            JPanel jb = new JPanel();
            jb.setLayout(new BoxLayout(jb, BoxLayout.X_AXIS));
            jb.add(startYb);
            jb.add(startFy);
            jPanel.add(jb);
            jPanel.add(label);
            JProgressBar progressBar = new JProgressBar(0, 100);
            progressBar.setStringPainted(true);
            jPanel.add(progressBar);
            startYb.addActionListener(e -> {
                startYb.setEnabled(false);
                startFy.setEnabled(false);
                new Thread(new ConvertTabel(oracleSessionFactory, dmSessionFactory, progressBar, table, true)).start();
            });
            startFy.addActionListener(e -> {
                startFy.setEnabled(false);
                startYb.setEnabled(false);
                new Thread(new ConvertTabel(oracleSessionFactory, dmSessionFactory, progressBar, table, false)).start();
            });
        }
        jFrame.add(jScrollPane);
        jFrame.setVisible(true);
    }
}
